/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test.test;

import android.app.Activity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.Editable;
import android.text.TextWatcher;
import static android.util.Log.v;
import static android.util.Log.v;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import modele.DAO;
import modele.Medecin;

/**
 *
 * @author sio
 */
public class NextActivity extends ListActivity {

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        TextView text1 = (TextView) findViewById(R.id.text1);
        Intent inter = getIntent();
        String num = inter.getStringExtra("leDep");
        text1.setText("Liste des médecins pour le département " + num);
        //lecture des noms
        final List<Medecin> lesMeds = DAO.getLesMeds(num);
//Création de l'adapter
        medAdapter adapter = new medAdapter(lesMeds, this);
        setListAdapter(adapter);
        Button send = (Button) findViewById(R.id.send);

        send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                EditText filter = (EditText) findViewById(R.id.filter);
                List<Medecin> lesMeds2 = new ArrayList<Medecin>();
                for (Medecin m : lesMeds) {
                    if (m.getNom().toUpperCase().startsWith(filter.getText().toString().toUpperCase())) {
                        lesMeds2.add(m);
                    }
                    medAdapter adapter = new medAdapter(lesMeds2, getApplicationContext());
                    setListAdapter(adapter);
                }

            }
        });

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        
        Medecin m = (Medecin) l.getItemAtPosition(position);
      final String contact = m.getTel();
      final String adresse = m.getAdresse().replace(" ","+");
      
      
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setButton("Téléphoner", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
       Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + contact));
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(callIntent);
   }
});
        
        alertDialog.setMessage("Appelez ou envoyez un SMS");
        
final EditText message = new EditText(this);
        
alertDialog.setView(message);
        alertDialog.setButton2("Envoyer un message", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(contact, null, message.getText().toString(), null, null);
        Toast.makeText(getApplicationContext(), "Votre SMS a bien été envoyé.",
                Toast.LENGTH_LONG).show();
   }
});
        
        
        
        alertDialog.setButton3("Géolocaliser", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      Intent inter = new Intent(getApplicationContext(), LocalisationActivity.class);
        inter.putExtra("adresse",adresse);
        startActivity(inter);
   }
});

alertDialog.show();
   /*     telephoner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + contact));
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(callIntent);
            }
        });
        
        messageTel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(contact, null, message.getText().toString(), null, null);
        Toast.makeText(getApplicationContext(), "SMS sent.",
                Toast.LENGTH_LONG).show();
            }
        }); */
        
    }
}
